home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 26 / macformat_26.iso / Shareware / Programación / C Reference Card / C Reference Card.rsrc / TEXT_414_14.txt < prev    next >
Text File  |  1997-01-29  |  4KB  |  89 lines

  1. I/O STREAMS : objects and manipulators, examples
  2. _________________________________________________________________________
  3.  
  4.  
  5. The standard input/output (I/O) library for C++ is <iostream.h>, not <stdio.h>.  Although C++ supports the "old" I/O functions, you should probably get use to using streams.  The following provides the basics of using C++ streams.
  6.  
  7.  
  8. OBJECTS AND MANIPULATORS
  9.  
  10.  
  11.                             C++ I/O OBJECTS
  12.                  =====================================
  13.                 |  Object Name      Default Device    |
  14.                 |-------------------------------------|
  15.                 |     cin              Keyboard       |
  16.                 |     cout              Screen        |
  17.                 |     cerr              Screen        |
  18.                 |     clog              Screen        |
  19.                  =====================================
  20.  
  21.  
  22.                           C++ I/O MANIPULATORS
  23.     ================================================================
  24.    |  Manipulator                               Description         |
  25.    |----------------------------------------------------------------|
  26.    |     dec                          Decimal output                |
  27.    |     hex                          Hexadecimal output            |          
  28.    |     oct                          Octal output                  |
  29.    |     endl                         End of line (or \n)           |
  30.    |     ends                         End of string (or \0)         |
  31.    |     flush                        Flushes buffer                |
  32.    |     resetiosflags(long)          Resets effect of setiosflags  |
  33.    |     setprecision( int )          Secifies digits of precision  |
  34.    |     setiosflags( format flag )   Sets format flag              |
  35.    |     setw( int )                  Sets field width              |
  36.     ================================================================
  37.  
  38.  
  39.                           C++ I/O FORMAT FLAGS
  40.     ================================================================
  41.    |  Format Flag                       Description                 |
  42.    |----------------------------------------------------------------|
  43.    |  ios::left          Left-justified output within width         |
  44.    |  ios::right         Right-justified output within width        |
  45.    |  ios::scientific    Formats numbers in scientific notation     |
  46.    |  ios::fixed         Normal decimal format                      |
  47.    |  ios::dec           Formats numbers in base 10                 |
  48.    |  ios::hex           Formats numbers in base 16                 |
  49.    |  ios::oct           Formats numbers in base 8                  |
  50.    |  ios::uppercase     Scientific notation characters uppercase   |
  51.    |  ios::showbase      Force base prefix in output                |
  52.    |  ios::showpos       Show '+' sign for positive numbers         |
  53.     ================================================================
  54.  
  55.  
  56.  
  57. EXAMPLES
  58.  
  59. The following snippet shows how to use the basics of using C++ I/O streams.  The insert operator '<<' and extraction operator '>>' are used to send and get data from the screen and keyboard.
  60.  
  61.   // io.cp
  62.   #include <iostream.h>
  63.   #include <iomanip.h>
  64.     
  65.   void main( void )
  66.   {
  67.     int   i;
  68.     float f;
  69.     char  c;
  70.     
  71.     cout << "Enter an integer: ";
  72.     cin >> i;
  73.     cout << "Enter a float: ";
  74.     cin >> f;
  75.     cout << "Enter a character: ";
  76.     cin >> c;
  77.     
  78.     cout << endl;
  79.     cout << "Default formats" << endl;
  80.     cout << i << endl;
  81.     cout << f << endl;
  82.     cout << c << endl;
  83.     
  84.     cout << endl << "Other formats" << endl;
  85.     cout << hex << i << endl;
  86.     cout << setprecision(2) << f << endl;
  87.     cout << setiosflags( ios::scientific ) << f << endl;
  88.   }
  89.   // end io.cp